package com.zuimei.zuimei.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.beans.FindAppItem4Bean;
import com.zuimei.zuimei.utils.ImgUtil;
import com.zuimei.zuimei.utils.ScreenUtil;

import java.util.List;

/**
 * Created by Jogger on 2017/1/18.
 * 发现应用--合辑
 */

public class Item4RecycleViewItemAdapter extends RecyclerView.Adapter<Item4RecycleViewItemAdapter
        .MyViewHolder> {
    private Context mContext;
    private List<FindAppItem4Bean> mContentList;

    public Item4RecycleViewItemAdapter(Context context) {
        mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.rv_item4_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        int width = ScreenUtil.getSrceenWidth(mContext) / 2;
        Picasso.with(mContext)
                .load(mContentList.get(position).getCover_image())
                .config(Bitmap.Config.RGB_565)
                .transform(ImgUtil.getTransformation(width))
                .into(holder.iv_content);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout
                .LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) holder.ll_content_container.getLayoutParams();
        if (position%2!=0){
            params.leftMargin=5;
            layoutParams.rightMargin=10;
        }else {
            layoutParams.leftMargin=10;
        }
        if (position<2){
            //头部空白
            params.topMargin=20;
        }
        holder.ll_content_container.setLayoutParams(layoutParams);
        holder.iv_content.setLayoutParams(params);
        holder.tv_title.setText(mContentList.get(position).getTitle());

    }

    public void loadData(List<FindAppItem4Bean> contentList) {
        mContentList = contentList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mContentList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_content;
        private TextView tv_title;
        private LinearLayout ll_content_container;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv_content= (ImageView) itemView.findViewById(R.id.iv_content);
            tv_title= (TextView) itemView.findViewById(R.id.tv_title);
            ll_content_container= (LinearLayout) itemView.findViewById(R.id.ll_content_container);
        }
    }
}
